<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use think\facade\Cache;

class Good extends BaseController
{
    public function cate()
    {
        $parentid=Request::get('id',0);
        $cate=Db::table('ecm_category')->order('sort')->where('parentid','=',$parentid)->select();
        //方案一：按照父辈ID进行判断
        $cateal=Db::table('ecm_category')->distinct(true)->column('parentid');
        View::assign('cateal',$cateal);

        View::assign('cate',$cate);
        return view();
    }

    public function cateedit()
    {
        return view();
    }

    public function goodAdd()
    {
        return view();
    }

    public function goodList()
    {
            $name=Request::param('keywords');
            $post=Request::post();
//            $is_hot=Request::param('is_hot');
//            $is_show=Request::param('is_show');


            $where=[];
            if(isset($name) && !empty($name))                       $where[]=['g.name','like',"%{$name}%",];
            if(isset($post['is_show']) && $post['is_show']<>'')     $where[]=['is_show','=',$post['is_show'],];
            if(isset($post['is_hot']) && $post['is_hot']<>'')       $where[]=['is_hot','=',$post['is_hot'],];

            if(isset($post['is_down']) && count($post['is_down'])<2)$where[]=['is_down','in',$post['is_down']];

            if (isset($post['addtime']) && $post['addtime']<>'')$where[]=['g.addtime','> TIME',$post['addtime']];
            if (isset($post['addtimes']) && $post['addtimes']<>'')$where[]=['g.addtime','< TIME',$post['addtimes']];

            if (isset($post['d_price']) && $post['d_price']<>'')$where[]=['g.price','>',$post['d_price']];
            if (isset($post['g_price']) && !empty($post['g_price']))$where[]=['g.price','<',$post['g_price']];

            if (isset($post['cid']) && !empty($post['cid']))$where[]=['g.cid','=',$post['cid']];


        //if(isset($is_down) && $is_down<>''){
            //if(count($is_down)<2){
            //    $where[]=['is_down','in',"{$is_down[0]}",];
            //}
            //}

            $list = Db::table('ecm_goods')
                ->alias('g')
                ->join('ecm_category c','g.cid = c.id')
                ->field('g.id,g.name,c.name as cname,price,photo_x,g.addtime,is_show,is_hot,is_down')
                ->where($where)
                ->select();
            View::assign('list',$list);
            //View::assign('name',$name);
//            View::assign('is_show',$is_show);
//            View::assign('is_hot',$is_hot);
            //View::assign('is_down',$is_down);
            View::assign($post);

            //方案一
            //$cate = Db::table('ecm_category')->where('parentid',0)->select()->toArray();
            //foreach ($categ as $v){
            //    $v['son'] = Db::table('ecm_category')->where('parentid',$v['id'])->select()->toArray();
            //
            //}

            //方案二
            //使用缓存
            if(Cache::has('cate')){ //判断缓存变量是否存在
                $cateAll = Cache::get('cate');  //获取缓存数据
            }else{
                $cateAll = Db::table('ecm_category')->select()->toArray();
                Cache::set('cate',$cateAll,100); //设置缓存变量
            }
            //2.1
            //$cate = [];
            ////循环所有分类  $v为每条分类
            //foreach ($cateAll as $v){
            //    //判断 分类中parentid字段等于0 进入判断结果 都是一级分类
            //    if($v['parentid'] == 0){
            //        //将$v一级分类赋值到空数组中  索引为自己的ID
            //        $cate[$v['id']] = $v;
            //        //循环所有分类
            //        foreach ($cateAll as $vv){
            //            //如果分类的parentid等于上一级分类(一级分类)ID 进入判断里的是二级分类
            //            if($vv['parentid'] == $v['id']){
            //                //将二级分类赋值到数组  根据索引($v.id) 将一级和二级绑定到一起
            //                $cate[$v['id']]['son'][$vv['id']] = $vv;
            //                //.......无限分类循环
            //            }
            //        }
            //    }
            //}

            //2.2
            $cate = $this->getCate($cateAll,0);
            View::assign('cate',$cate);

        return view();
    }

    public function getCate($cateAll,$parentid)
    {
        $cate = [];
        foreach ($cateAll as $v){
            if($v['parentid'] == $parentid){
                $cate[$v['id']] = $v;
                $cate[$v['id']]['son'] = $this->getCate($cateAll,$v['id']);
            }
        }
        return $cate;
    }

    public function goodEdit()
    {
        return view();
    }
}










